﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BasePlatformCommon;

namespace L9SubSet.WriteAddrPro
{
	public partial class Form_SubParaWriteAddrPro : Form
	{
		// 参数传递对象
		public SubParaWriteAddrPro subParaWriteAddrPro = null;

		// 端口点数 TextBox 控件集合
		private List<TextBox> lampNumTextBoxList = new List<TextBox>();

		private string section = "WriteProSubPara";			// 多语言文件配置节名称

		private string tip = "提示";							// MessageBox 题头

		/*****************************************************************************************/

		// 构造函数
		public Form_SubParaWriteAddrPro(SubParaWriteAddrPro subParaWriteAddrPro, string mode)
		{
			InitializeComponent();

			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
			this.Font = new System.Drawing.Font(this.Font.Name, 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel, ((byte)(134)));

			lampNumTextBoxList.Add(lampNumTextBox1);
			lampNumTextBoxList.Add(lampNumTextBox2);
			lampNumTextBoxList.Add(lampNumTextBox3);
			lampNumTextBoxList.Add(lampNumTextBox4);
			lampNumTextBoxList.Add(lampNumTextBox5);
			lampNumTextBoxList.Add(lampNumTextBox6);
			lampNumTextBoxList.Add(lampNumTextBox7);
			lampNumTextBoxList.Add(lampNumTextBox8);

			this.subParaWriteAddrPro = subParaWriteAddrPro;
			subIndexTextBox.ReadOnly = true;
			subIndexTextBox.Text = (subParaWriteAddrPro.subIndex + 1).ToString();
			
			for (int i = 0; i < 8; i++)
			{
				lampNumTextBoxList[i].Text = subParaWriteAddrPro.lampNum[i].ToString();
			}

			if (mode.Equals("add"))
			{
				allLampNumCheckBox.Checked = true;
			}

			// 设置本地化语言
			if (LanguageOper.available == true)
			{
				setLanguageText();
			}
		}

		// 设置本地化语言
		public void setLanguageText()
		{
			if (LanguageOper.available == false)
			{
				return;
			}

			LanguageOper.SetCurrentCulture();

			tip = LanguageOper.ReadValue("Common", "tipStr");

			// 界面控件文字
			this.Text = LanguageOper.ReadValue(section, "writeProSubPara.Text");
			subLabel.Text = LanguageOper.ReadValue(section, "subLabel.Text");
			allLampNumCheckBox.Text = LanguageOper.ReadValue(section, "allLampNumCheckBox.Text");
			portLabel1.Text = LanguageOper.ReadValue(section, "portLabel1.Text");
			portLabel2.Text = LanguageOper.ReadValue(section, "portLabel2.Text");
			portLabel3.Text = LanguageOper.ReadValue(section, "portLabel3.Text");
			portLabel4.Text = LanguageOper.ReadValue(section, "portLabel4.Text");
			portLabel5.Text = LanguageOper.ReadValue(section, "portLabel5.Text");
			portLabel6.Text = LanguageOper.ReadValue(section, "portLabel6.Text");
			portLabel7.Text = LanguageOper.ReadValue(section, "portLabel7.Text");
			portLabel8.Text = LanguageOper.ReadValue(section, "portLabel8.Text");
			okButton.Text = LanguageOper.ReadValue(section, "okButton.Text");
			cancelButton.Text = LanguageOper.ReadValue(section, "cancelButton.Text");
			if (LanguageOper.LocalLanguage != LanguageEnum.EN)
			{
				allLampNumCheckBox.Location = new Point(25, 43);
				portLabel1.Location = new Point(38, 74);
				portLabel2.Location = new Point(38, 103);
				portLabel3.Location = new Point(38, 132);
				portLabel4.Location = new Point(38, 161);
				portLabel5.Location = new Point(38, 190);
				portLabel6.Location = new Point(38, 219);
				portLabel7.Location = new Point(38, 248);
				portLabel8.Location = new Point(38, 277);
			}
			else
			{
				allLampNumCheckBox.Location = new Point(14, 43);
				portLabel1.Location = new Point(30, 74);
				portLabel2.Location = new Point(30, 103);
				portLabel3.Location = new Point(30, 132);
				portLabel4.Location = new Point(30, 161);
				portLabel5.Location = new Point(30, 190);
				portLabel6.Location = new Point(30, 219);
				portLabel7.Location = new Point(30, 248);
				portLabel8.Location = new Point(30, 277);
			}
		}

		// 检查数据合法性
		private bool checkInputData()
		{
			for (int i = 0; i < 8; i++)
			{
				int tmpLampNum = -1;
				bool res = int.TryParse(lampNumTextBoxList[i].Text.Trim(), out tmpLampNum);
				if(res == false || tmpLampNum < 0 || tmpLampNum > 512)
				{
					if (allLampNumCheckBox.Checked)
					{
						string msg = "填写错误：单个端口的灯点数应在 [0,512] 范围";
						if (LanguageOper.available == true)
						{
							msg = LanguageOper.ReadValue(section, "msgStr.0");
						}
						MessageBoxEx.Show(this, msg, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
					}
					else
					{
						string msg = string.Format("填写错误：端口 {0} 的灯点数应在 [0,512] 范围", i + 1);
						if (LanguageOper.available == true)
						{
							msg = string.Format(LanguageOper.ReadValue(section, "msgStr.1"), i + 1);
						}
						MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.Information);
					}
					return false;
				}
				else
				{
					subParaWriteAddrPro.lampNum[i] = tmpLampNum;
				}
			}

			return true;
		}

		// 统一设置点数
		private void allLampNumCheckBox_CheckedChanged(object sender, EventArgs e)
		{
			allLampNumTextBox.Enabled = allLampNumCheckBox.Checked;
			foreach (TextBox testBox in lampNumTextBoxList)
			{
				testBox.Enabled = !allLampNumCheckBox.Checked;
			}
		}

		// 全局点数变化
		private void allLampNumTextBox_TextChanged(object sender, EventArgs e)
		{
			foreach (TextBox testBox in lampNumTextBoxList)
			{
				testBox.Text = allLampNumTextBox.Text;
			}
		}

		// 确定
		private void okButton_Click(object sender, EventArgs e)
		{
			if (checkInputData() == false)
			{
				return;
			}
			else
			{
				this.DialogResult = System.Windows.Forms.DialogResult.OK;
			}
		}

		// 取消
		private void cancelButton_Click(object sender, EventArgs e)
		{
			this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
		}
	}
}
